#include "StdAfx.h"
#include "pipe_impl.h"
#include "pipe_protocol.h"

namespace utility
{
namespace network
{
pipe_impl::pipe_impl(PIPEID local_id, PIPEID remote_id, session * s) :
    local_id_(local_id), remote_id_(remote_id), session_(s)
{
    event_handler_ = NULL;
}

PIPEID pipe_impl::get_local_id()
{
    return local_id_;
}

PIPEID pipe_impl::get_remote_id()
{
    return remote_id_;
}

std::string pipe_impl::get_remote_ip_as_string()
{
    return session_->get_remote_ip_as_string();
}

void pipe_impl::set_pipe_event_handler(pipe_event_handler * e)
{
    event_handler_ = e;
}

void pipe_impl::send(void *message, size_t message_length)
{
    size_t buf_size = message_length + sizeof(pipe_protocol_header);
    char * buf = (char *)alloca(buf_size);
    pipe_protocol_header * header = (pipe_protocol_header*)buf;
    header->op_type = 0;
    memcpy(buf + sizeof (pipe_protocol_header), message, message_length);
    session_->send(buf, buf_size);
}

pipe_event_handler * pipe_impl::get_event_handler()
{
    return event_handler_;
}

uint32 pipe_impl::get_remote_ip()
{
    return session_->get_remote_ip();
}

}   // end of namespace network
}   // end of namespace utility
